package net.zjitc.reggiebymeme.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import net.zjitc.reggiebymeme.dto.DishDto;
import net.zjitc.reggiebymeme.entity.Category;
import net.zjitc.reggiebymeme.entity.Dish;
import net.zjitc.reggiebymeme.entity.DishFlavor;
import net.zjitc.reggiebymeme.mapper.DishMapper;
import net.zjitc.reggiebymeme.service.CategoryService;
import net.zjitc.reggiebymeme.service.DishFlavorService;
import net.zjitc.reggiebymeme.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    @Transactional
    @Override
    public void saveWithFlavor(DishDto dishDto){
    this.save(dishDto);
        Long dishId = dishDto.getId();
        //菜品id
        List<DishFlavor> flavors = dishDto.getFlavors();
        //菜品⼝味
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
         //保存菜品⼝味数据到菜品⼝味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public Page<DishDto> pageWithCategoryName(Integer page, Integer pageSize, String name) {
        return null;
    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {

    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        Dish dish =this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        //从dish表查询菜品基本信息

        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);
        //在从dish_flavor表查询询当前菜品对应的⼝味信息
        return dishDto;
    }
}
